﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using GameServer.Network;
using GameServer.Model;

namespace GameServer
{
    class Server
    {
        TcpListener _listener = null;

        World _world = null;

        public Server(int port)
        {
            ThreadPool.SetMinThreads(2, 2);
            int maxThreads = Environment.ProcessorCount * 64;
            ThreadPool.SetMaxThreads(maxThreads,maxThreads);

            _world = new World();

            _listener = new TcpListener(IPAddress.Any, port);
            _listener.Start();

            while (true)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(ClientThread), _listener.AcceptTcpClient());           
            }
        }
        
        static void ClientThread(Object StateInfo)
        {
            new Client((TcpClient)StateInfo);
        }

        ~Server()
        {
            if (_listener != null)
            {
                _listener.Stop();
            }

        }

        static void Main(string[] args)
        {
            new Server(7878);
        }
    }
}
